#include "background.h"
#include "GraphicsArea/drawingarea.h"

Background::Background(DrawingArea* drawing, int width, int height){
    setPos(0, 0);
    widthBoundingRect = width;
    heightBoundingRect = height;
    this->drawing = drawing;
    setZValue(-1);
}

void Background::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    int sizeSkare = 20;

    qreal scale = drawing->getScale();

    sizeSkare = sizeSkare / scale;

    painter->setPen(QColor(150, 150, 150));

    painter->setBrush(QBrush(QColor(245, 245, 245), Qt::SolidPattern));
    painter->drawRect(0, 0, drawing->getWidth(), drawing->getHeight());

    painter->setBrush(QBrush(QColor(100, 100, 100), Qt::SolidPattern));
    for(int i = 0; i < drawing->getWidth(); i+= sizeSkare * 2){
        int j;
        for(j = 0; j < drawing->getHeight(); j+= sizeSkare * 2){
            int endPointX = min(i + sizeSkare, drawing->getWidth());
            int endPointY = min(j + sizeSkare, drawing->getHeight());
            painter->drawRect(i, j, endPointX - i, endPointY - j);
        }   
    }

    for(int i = sizeSkare; i < drawing->getWidth(); i+= sizeSkare * 2){
        for(int j = sizeSkare; j < drawing->getHeight(); j+= sizeSkare * 2){
            int endPointX = min(i + sizeSkare, drawing->getWidth());
            int endPointY = min(j + sizeSkare, drawing->getHeight());
            painter->drawRect(i, j, endPointX - i, endPointY - j);
        }
    }
}

QRectF Background::boundingRect() const{
    return QRectF(0, 0, drawing->getWidth(), drawing->getHeight());
}

